package com.tianyang.yukuaifuzs.business.auth.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.auth.bean.MerchantVerifyOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.business.auth.contract.MerchantEmpowerContract;
import com.tianyang.yukuaifuzs.business.auth.presenter.MerchantEmpowerPresenter;
import com.tianyang.yukuaifuzs.common.utils.CommonUtils;
import com.tianyang.yukuaifuzs.common.utils.CommonViewUtils;
import com.tianyang.yukuaifuzs.module.roundview.RoundTextView;
import com.tianyang.yukuaifuzs.utils.DownloadImgUtils;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsMgr;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：授权
 * Created by xyj on 2024/3/8
 */
public class MerchantEmpowerActivity extends BaseTitleActivity implements MerchantEmpowerContract.View {

    private String permission = PermissionsConstant.WRITE_STORAGE;

    @BindView(R.id.leftTitleTv)
    TextView leftTitleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.shareViewRl)
    FrameLayout shareViewRl;
    @BindView(R.id.wxCodeSdv)
    SimpleDraweeView wxCodeSdv;
    @BindView(R.id.aliCodeSdv)
    SimpleDraweeView aliCodeSdv;

    @BindView(R.id.singleCodeRtv)
    RoundTextView singleCodeRtv;
    @BindView(R.id.doubleCodeLl)
    LinearLayout doubleCodeLl;
    @BindView(R.id.leftCodeRtv)
    RoundTextView leftCodeRtv;
    @BindView(R.id.rightCodeRtv)
    RoundTextView rightCodeRtv;

    private MerchantEmpowerContract.Presenter mPresenter;

    private MerchantVerifyOutput mOutput = new MerchantVerifyOutput();

    private boolean isShowAli;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_merchant_empower;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("授权");
        String phone = getIntent().getStringExtra(AuthConfig.INTENT_USERNAME);
        mPresenter = new MerchantEmpowerPresenter(this, phone);
        mPresenter.getMerchantVerify();
    }

    @OnClick({R.id.leftTitleTv, R.id.rightTitleTv, R.id.copyTv, R.id.downloadTv, R.id.shareTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.leftTitleTv:
                if (isShowAli) {
                    isShowAli = false;
                    leftTitleTv.setBackgroundResource(R.drawable.img_left_choose);
                    leftTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    rightTitleTv.setBackgroundResource(R.drawable.img_right_normal);
                    rightTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    wxCodeSdv.setVisibility(View.VISIBLE);
                    aliCodeSdv.setVisibility(View.GONE);
                    if (mOutput != null) {
                        showCodeView(mOutput.getWeChatCode(), false);
                    }
                }
                break;
            case R.id.rightTitleTv:
                if (!isShowAli) {
                    isShowAli = true;
                    leftTitleTv.setBackgroundResource(R.drawable.img_left_normal);
                    leftTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    rightTitleTv.setBackgroundResource(R.drawable.img_right_choose);
                    rightTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    wxCodeSdv.setVisibility(View.GONE);
                    aliCodeSdv.setVisibility(View.VISIBLE);
                    if (mOutput != null) {
                        showCodeView(mOutput.getAliCode(), true);
                    }
                }
                break;
            case R.id.copyTv:
                CommonUtils.copyText(mContext, isShowAli ? mOutput.getAliCode() : mOutput.getWeChatCode());
                break;
            case R.id.downloadTv:
                if (!PermissionsMgr.getInstance().hasPermission(mContext, permission)) {
                    requestPermissions(permission);
                    return;
                }
                downloadClick();
                break;
            case R.id.shareTv:
                shareClick();
                break;
        }
    }

    private void shareClick() {
        shareViewRl.postDelayed(new Runnable() {
            @Override
            public void run() {
                shareViewRl.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = shareViewRl.getDrawingCache();
                if (mClipScreen == null || mClipScreen.isRecycled()) return;
                CommonViewUtils.shareToWeChat(mContext, mClipScreen);
            }
        }, 500);
    }

    private void downloadClick() {
        shareViewRl.postDelayed(new Runnable() {
            @Override
            public void run() {
                shareViewRl.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = shareViewRl.getDrawingCache();
                if (mClipScreen == null || mClipScreen.isRecycled()) return;
                boolean isSuccess = DownloadImgUtils.saveBitmap(mContext, mClipScreen);
                if (isSuccess) {
                    ToastUtil.show("二维码保存成功！");
                } else {
                    ToastUtil.show("二维码保存失败, 请重试！");
                }
            }
        }, 500);
    }

    @Override
    public void onPermissionGranted(List<String> permissions) {
        if (permissions != null && permissions.contains(permission)) {
            downloadClick();
        }
    }

    @Override
    public void showMerchantVerifyView(MerchantVerifyOutput output) {
        if (output != null) {
            mOutput = output;
            if (!TextUtils.isEmpty(output.getWeChatUrl())) {
                wxCodeSdv.setImageURI(Uri.parse(output.getWeChatUrl()));
            }
            if (!TextUtils.isEmpty(output.getAliUrl())) {
                aliCodeSdv.setImageURI(Uri.parse(output.getAliUrl()));
            }
            showCodeView(output.getWeChatCode(), false);
        }
    }

    private void showCodeView(String code, boolean isAli) {
        shareViewRl.destroyDrawingCache();
        if (isAli && !TextUtils.isEmpty(code) && code.contains(",") && code.split(",").length > 1) {
            String[] codes = code.split(",");
            singleCodeRtv.setVisibility(View.GONE);
            doubleCodeLl.setVisibility(View.VISIBLE);
            leftCodeRtv.setText(VerifyUtil.checkString(codes[0]));
            rightCodeRtv.setText(VerifyUtil.checkString(codes[1]));
            return;
        }
        singleCodeRtv.setVisibility(View.VISIBLE);
        singleCodeRtv.setText(VerifyUtil.checkString(code));
        doubleCodeLl.setVisibility(View.GONE);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantEmpowerContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
